package cn.wolfcode.luowowo.website.web.util;

import cn.wolfcode.luowowo.cache.util.RedisKeysUtil;
import cn.wolfcode.luowowo.common.exception.LogicException;
import jdk.nashorn.internal.parser.Token;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static String getToken(HttpServletRequest req,HttpServletResponse resp){
        Cookie[] cookies = req.getCookies();
        if(cookies != null && cookies.length>0){
            for (Cookie cookie : cookies) {
                if( "token".equals(cookie.getName())){
                    String token = cookie.getValue();
                    //cookie.setPath("/");
                    //cookie.setMaxAge(RedisKeysUtil.LOGIN_USER.getTime().intValue());
                    //addCookie("token",token,resp);
                    //resp.addCookie(cookie);
                    return token;
                }
            }
        }
       return null;
    }

    public static void addCookie(String key, String value, HttpServletResponse resp){
        Cookie cookie = new Cookie(key,value);
        //5.设置有效路径
        cookie.setPath("/");
        //6.设置有效时间
        cookie.setMaxAge(RedisKeysUtil.LOGIN_USER.getTime().intValue());
        resp.addCookie(cookie);
    }
}
